package com.example.hao.day9_14;

import android.graphics.Color;

import android.hardware.fingerprint.FingerprintManager;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v4.os.CancellationSignal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show = findViewById(R.id.tv_show);
    }

    /**
     * 是否可以使用指纹识别
     *
     * @param manger 指纹管理对象
     * @return
     */
    private boolean canUseFingerprint(FingerprintManagerCompat manger) {
        if (!manger.isHardwareDetected()) {//手机是否有指纹识别模块
            return false;
        }
        return manger.hasEnrolledFingerprints();//是否录入指纹
    }

    public void test(View view) {
        tv_show.setTextColor(Color.BLACK);
        tv_show.setText("开始验证指纹...");
        //获取指纹管理对象
        FingerprintManagerCompat fmc = FingerprintManagerCompat.from(this);

        if (!canUseFingerprint(fmc)) {
            return;
        }

        cancellationSignal = new CancellationSignal();
        fmc.authenticate(null, 0, cancellationSignal, new FingerprintCallback(), null);
    }

    private CancellationSignal cancellationSignal;

    /**
     * 取消按钮
     *
     * @param view
     */
    public void cancel(View view) {
        if (!cancellationSignal.isCanceled()) {
            cancellationSignal.cancel();
        }

    }


    class FingerprintCallback extends FingerprintManagerCompat.AuthenticationCallback {


        /**
         * 当多次验证失败后 或 被用户取消,该方法被调用,并且在将来的一段时间内不能再使用指纹验证
         *
         * @param errMsgId
         * @param errString
         */
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            super.onAuthenticationError(errMsgId, errString);
            switch (errMsgId) {
                case FingerprintManager.FINGERPRINT_ERROR_CANCELED:
                    tv_show.setText("取消指纹识别:" + errMsgId + "," + errString);
                    break;
                case FingerprintManager.FINGERPRINT_ERROR_LOCKOUT:
                    tv_show.setText("多次验证失败:" + errMsgId + "," + errString);
                    break;
                default:
                    tv_show.setText("未知错误!");
            }

        }

        /**
         * 在验证过程中,遇到可恢复的异常时被调用.(传感器脏,请把它擦干净...)
         *
         * @param helpMsgId
         * @param helpString
         */
        @Override
        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
            super.onAuthenticationHelp(helpMsgId, helpString);
            tv_show.setText("指纹模块异常:" + helpMsgId + "," + helpString);
        }

        /**
         * 指纹验证成功
         *
         * @param result
         */
        @Override
        public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            tv_show.setText("指纹验证成功!");
            tv_show.setTextColor(Color.GREEN);
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            tv_show.setText("指纹验证失败!");
            tv_show.setTextColor(Color.RED);
        }
    }
}
